<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>

</head>
<body>
    <button id="btn">提交</button>
    <button id="btn2">取消提交</button>
    <script>
        //普通axios请求
       /*  btn.onclick=function(){
            axios({
                method:"get",
                url:'/get',
                params:{
                    userId:"001"
                },
                headers:{},
                timeout:10000
            })
        } */

        //axios别名请求
        /* btn.onclick=function(){
            axios.get('http://127.0.0.1:8090/axios/get',{
                params:{
                    userId:'001'
                },
                timeout:10000
            })
        } */
        //axios实例请求
        /* const myAxios = axios.create({
            baseURL:'http://127.0.0.1:8090/axios',
            timeout:10000,
            headers:{}
        })
        btn.onclick=function(){
            myAxios.get('/get',{
                params:{
                    userId:'001'
                },
                
            })
        } */
        //设置拦截器
        /* const myAxios = axios.create({
            headers:{},
            baseURL:'http://127.0.0.1:8090/axios',
            timeout:10000
        }) 

        myAxios.interceptors.request.use((config)=>{
            return config
        })
        myAxios.interceptors.request.use((response)=>{
            return response
        },(err)=>{
            return Promise.reject(err)
        })
        btn.onclick=function(){
            myAxios.get('/get',{
                params:{
                    userId:'001'
                },
                
            })
        } */
        //取消请求
        const myAxios = axios.create({
            headers:{},
            timeout:10000,
            baseURL:'http://127.0.0.1:8090/axios'
        })

        const CancelToken = axios.CancelToken
        let cancel = null
        btn.onclick=function(){
            myAxios.get('/get',{
                params:{
                    userId:'001'
                },
                cancelToken:new CancelToken((e)=>{
                    cancel = e
                })
            })
        }
        btn2.onclick=function(){
            cancel()
        }
    </script>
</body>
</html>